﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Vant.WpfControls;
using Vant.WpfControls.DrawingObject;

namespace Vant.CellTypes
{
    public class InputCellTypeBaseDesigner<T> : CellTypeDesigner<T>, ISupportPropertyInitialize where T : InputCellTypeBase
    {
        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            var inputDrawing = new InputDrawingObject(StyleHelper.CreateStyleViewModel(cellInfo, drawingHelper));
            var viewModel = inputDrawing.ViewModel;
            switch (cellInfo.HorizontalAlignment)
            {
                case ForguncyCellHorizontalAlignment.Center:
                    viewModel.HorizontalAlignment = HorizontalAlignment.Center;
                    break;
                case ForguncyCellHorizontalAlignment.Right:
                    viewModel.HorizontalAlignment = HorizontalAlignment.Right;
                    break;
                default:
                    break;
            }
            viewModel.placeholder = CellType.placeholder;
            viewModel.leftIcon = IconHelper.GetIcon(CellType.leftIcon, drawingHelper);
            viewModel.rightIcon = IconHelper.GetIcon(CellType.rightIcon, drawingHelper);
            viewModel.arrowIcon = IconHelper.GetIcon(new ImageValue()
            {
                Name = "Arrow/292_move_next_3.svg",
                BuiltIn = true,
                Color = "#969799"
            }, drawingHelper);
            viewModel.border = CellType.border;
            viewModel.IsDisabled = CellType.IsDisabled;
            var text = FormatText(cellInfo);
            viewModel.text = drawingHelper.GetHelpTextForPage() ?? text;
            return inputDrawing;
        }

        public virtual string FormatText(ICellInfo cellInfo)
        {
            return cellInfo.Text;
        }

        public virtual void InitDefaultPropertyValues(IBuilderContext context)
        {
            this.CellType.placeholder = Properties.Resources.Vant_PleaseSelect;
        }
    }

    public abstract class InputCellTypeBase : VantCellTypeBase, ICommandCellType, ISupportUIPermission, ISupportDataValidation, ISupportDefaultValue
    {
        [SRDisplayName(nameof(Properties.Resources.Vant_EditCommands))]
        public virtual List<Command> CommandList { get; set; } = new List<Command>();
        public CommandExcuteKind CommandExcuteKind => CommandExcuteKind.OnValueChanged;

        [SRDisplayName(nameof(Properties.Resources.Vant_DataValidation))]
        [JsonIgnore]
        [DefaultValue(null)]
        public virtual DataValidationLink DataValidationLink { get; set; }

        List<UIPermission> _uIPermissions;
        [SRDisplayName(nameof(Properties.Resources.Vant_UIPermissions))]
        [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)]
        public virtual List<UIPermission> UIPermissions
        {
            get
            {
                if (_uIPermissions == null)
                {
                    _uIPermissions = Utils.GetDefaultPermission(UIPermissionScope.Enable, UIPermissionScope.Visible, UIPermissionScope.Editable);
                }
                return _uIPermissions;
            }
            set
            {
                _uIPermissions = value;
            }
        }
        [SRDisplayName(nameof(Properties.Resources.Vant_DefaultValue))]
        [FormulaProperty]
        public virtual object DefaultValue { get; set; }

        [SRDisplayName(nameof(Properties.Resources.Vant_Placeholder))]
        public virtual string placeholder { get; set; }

        [SRDisplayName(nameof(Properties.Resources.Vant_LeftIcon))]
        [IconProperty]
        public virtual ImageValue leftIcon { get; set; }

        [SRDisplayName(nameof(Properties.Resources.Vant_RightIcon))]
        [IconProperty]
        public virtual ImageValue rightIcon { get; set; }

        [SRCategoryHeader(nameof(Properties.Resources.Other))]
        [SRDisplayName(nameof(Properties.Resources.Vant_ShowInnerBorder))]
        [DefaultValue(true)]
        public virtual bool border { get; set; } = true;

        [SRDisplayName(nameof(Properties.Resources.Vant_EnableClearIcon))]
        public virtual bool clearable { get; set; }

        [SRDisplayName(nameof(Properties.Resources.Vant_Readonly))]
        public virtual bool ReadOnly { get; set; }

        [SRDisplayName(nameof(Properties.Resources.Vant_Disable))]
        public virtual bool IsDisabled { get; set; }

        public override SupportFeatures SupportFeatures
        {
            get
            {
                return SupportFeatures.ShouldCheckDirtyWhenLeavePage;
            }
        }

        public virtual bool NeedFormatDefaultValue => false;

        public override ForguncyDisabledSettings GetDisabledPropertySettings(ForguncyPageKind pageType)
        {
            return ForguncyDisabledSettings.TopAlignment |
                ForguncyDisabledSettings.BottomAlignment |
                ForguncyDisabledSettings.MiddleAlignment;
        }
    }
}
